package com.lemon.tuvi2015.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.lemon.tuvi2015.object.DetailsNotRuoiObject;
import com.lemon.tuvi2015.object.DetailsObject;
import com.lemon.tuvi2015.object.GiachuObject;
import com.lemon.tuvi2015.object.GlobalObject;
import com.lemon.tuvi2015.object.XongdatObject;

public class JsonParser {

	public static String getJson(String url) {
		InputStream is = null;
		String result = "";
		JSONObject jsonObject = null;

		// HTTP
		try {
			HttpClient httpclient = new DefaultHttpClient(); // for port 80
																// requests!
			HttpPost httppost = new HttpPost(url);
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			is = entity.getContent();
		} catch (Exception e) {
			// Log.e("getjson", "HTTP++++++++++++++++++++++++++++++++++++++");
			e.printStackTrace();
			return null;
		}
		// Read response to string
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();
			result = sb.toString();
		} catch (Exception e) {
			// Log.e("getjson",
			// "Read response to string=======================================");
			e.printStackTrace();
			return null;
		}

		// // Convert string to object
		// try {
		// jsonObject = new JSONObject(result);
		// } catch (JSONException e) {
		// return null;
		// }

		return result;

	}

	public static String getJson(String cmd, String body) {
		String url = String.format(CMD.URL_MAIN, cmd, body);
		// Log.e("getJson", "cmd: " + cmd);
		// Log.e("getJson", "body: " + body);
		// Log.e("getJson", "url: " + url);
		return getJson(url);
	}

	public static String getAdmobID() {
		String url = String.format(CMD.URL_MAIN, "1619", "&body=ANDROID");
		// Log.e("getJson", "cmd: " + cmd);
		// Log.e("getJson", "body: " + body);
		// Log.e("getJson", "url: " + url);
		return getJson(url);
	}

	public static String boinotruoi(String type, String position) {
		String body = String.format("{'not':'%s','type':'%s'}", position, type);
		// Log.e("getJson", "body: " + body);
		try {
			body = URLEncoder.encode(body, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String url = String.format(CMD.URL_MAIN, CMD.BOI_NOT_RUOI, body);
		// Log.e("getJson", "cmd: " + CMD.BOI_NOT_RUOI);
		// Log.e("getJson", "body: " + body);
		// Log.e("getJson", "url: " + url);
		return getJson(url);
	}

	public static String xongdat(String name, String birthday) {
		String body = String.format("{'name':'%s','bthday':'%s'}", name, birthday);
		// Log.e("getJson", "body: " + body);
		try {
			body = URLEncoder.encode(body, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String url = String.format(CMD.URL_MAIN, CMD.XONG_DAT, body);
		// Log.e("getJson", "cmd: " + CMD.BOI_NOT_RUOI);
		// Log.e("getJson", "body: " + body);
		// Log.e("getJson", "url: " + url);
		return getJson(url);
	}

	public static ArrayList<String> readAmobID(String result) throws JSONException {
		ArrayList<String> arrayList=new ArrayList<String>();
		JSONObject jsonObject = new JSONObject(result);
//		arrayList.add(Integer.toString(jsonObject.getInt("success")));
		JSONArray jsonArrayMenu = jsonObject.getJSONArray("data");
		for (int i = 0; i < jsonArrayMenu.length(); i++) {
			JSONObject jsonObj = jsonArrayMenu.getJSONObject(i);
			String banner="TOP_BANNER";
			String full="BIG_BANNER";
			if (!jsonObj.isNull(banner))
				arrayList.add(jsonObj.getString(banner));
			if (!jsonObj.isNull(full))
				arrayList.add(jsonObj.getString(full));
		}
		return arrayList;
	}
	
	public static String readStatus(String result) throws JSONException {
		String status = "";
		JSONObject jsonObject = new JSONObject(result);
		status = Integer.toString(jsonObject.getInt("success"));
		return status;
	}

	public static ArrayList<GlobalObject> readlistZodiac(String result) throws JSONException {
		JSONObject jsonObject = new JSONObject(result);
		ArrayList<GlobalObject> listZodiac = new ArrayList<GlobalObject>();
		JSONArray jsonArrayMenu = jsonObject.getJSONArray("data");
		for (int i = 0; i < jsonArrayMenu.length(); i++) {
			JSONObject jsonObj = jsonArrayMenu.getJSONObject(i);
			GlobalObject zodiac = new GlobalObject();
			if (!jsonObj.isNull(TagName.id))
				zodiac.setID(jsonObj.getString(TagName.id));
			if (!jsonObj.isNull(TagName.txname))
				zodiac.setNAME(jsonObj.getString(TagName.txname));
			if (!jsonObj.isNull(TagName.avatar))
				zodiac.setIMAGE(jsonObj.getString(TagName.avatar));
			if (!jsonObj.isNull(TagName.code))
				zodiac.setCODE(jsonObj.getString(TagName.code));
			// zodiac.showinfo();
			listZodiac.add(zodiac);
		}
		return listZodiac;
	}

	public static ArrayList<DetailsObject> readDetails(String result) throws JSONException {
		JSONObject jsonObject = new JSONObject(result);
		ArrayList<DetailsObject> arrayListDetails = new ArrayList<DetailsObject>();
		JSONArray array = jsonObject.getJSONArray("data");
		for (int i = 0; i < array.length(); i++) {
			JSONObject object = array.getJSONObject(i);
			DetailsObject detailsObject = new DetailsObject();
			if (!object.isNull(TagName.content))
				detailsObject.setContent(object.getString(TagName.content));
			if (!object.isNull(TagName.title))
				detailsObject.settitle(object.getString(TagName.title));
			if (!object.isNull(TagName.title))
				detailsObject.settitle(object.getString(TagName.title));
			if (!object.isNull(TagName.txname))
				detailsObject.settxname(object.getString(TagName.txname));
			if (!object.isNull(TagName.date))
				detailsObject.setdate(object.getString(TagName.date));
			if (!object.isNull(TagName.id_comtent))
				detailsObject.setid_comtent(object.getString(TagName.id_comtent));

			arrayListDetails.add(detailsObject);
		}
		return arrayListDetails;
	}

	public static ArrayList<DetailsNotRuoiObject> readNotRuoi(String result) throws JSONException {
		JSONObject jsonObject = new JSONObject(result);
		ArrayList<DetailsNotRuoiObject> arrayListDetails = new ArrayList<DetailsNotRuoiObject>();
		JSONArray array = jsonObject.getJSONArray("data");
		for (int i = 0; i < array.length(); i++) {
			JSONObject object = array.getJSONObject(i);
			DetailsNotRuoiObject detailsObject = new DetailsNotRuoiObject();
			if (!object.isNull("id_"))
				detailsObject.setid(object.getString("id_"));
			if (!object.isNull(TagName.txname))
				detailsObject.settxname(object.getString(TagName.txname));
			if (!object.isNull(TagName.title))
				detailsObject.settitle(object.getString(TagName.title));
			if (!object.isNull(TagName.content))
				detailsObject.setContent(object.getString(TagName.content));
			if (!object.isNull("values"))
				detailsObject.setvalues(object.getString("values"));
			if (!object.isNull(TagName.avatar))
				detailsObject.setavatar(object.getString(TagName.avatar));
			arrayListDetails.add(detailsObject);
		}
		return arrayListDetails;
	}

	public static ArrayList<GlobalObject> readlistSms(String result) throws JSONException {
		JSONObject jsonObject = new JSONObject(result);
		ArrayList<GlobalObject> listZodiac = new ArrayList<GlobalObject>();
		JSONArray jsonArrayMenu = jsonObject.getJSONArray("data");
		for (int i = 0; i < jsonArrayMenu.length(); i++) {
			JSONObject jsonObj = jsonArrayMenu.getJSONObject(i);
			GlobalObject zodiac = new GlobalObject();
			if (!jsonObj.isNull("id_sms"))
				zodiac.setID(jsonObj.getString("id_sms"));
			if (!jsonObj.isNull(TagName.txname))
				zodiac.setNAME(jsonObj.getString(TagName.txname));
			if (!jsonObj.isNull(TagName.avatar))
				zodiac.setIMAGE(jsonObj.getString(TagName.avatar));
			// zodiac.showinfo();
			listZodiac.add(zodiac);
		}
		return listZodiac;
	}

	public static ArrayList<GiachuObject> readgiachu(String result) throws JSONException {
		JSONObject jsonObject = new JSONObject(result);
		ArrayList<GiachuObject> listZodiac = new ArrayList<GiachuObject>();
		JSONArray jsonArrayMenu = jsonObject.getJSONArray("GIA_CHU");
		for (int i = 0; i < jsonArrayMenu.length(); i++) {
			JSONObject jsonObj = jsonArrayMenu.getJSONObject(i);
			GiachuObject zodiac = new GiachuObject();
			if (!jsonObj.isNull("tx_name"))
				zodiac.setname(jsonObj.getString("tx_name"));
			if (!jsonObj.isNull("a_lich"))
				zodiac.setamlich(jsonObj.getString("a_lich"));
			if (!jsonObj.isNull("d_lich"))
				zodiac.setduonglich(jsonObj.getString("d_lich"));
			if (!jsonObj.isNull("ngu_hang"))
				zodiac.setnguhanh(jsonObj.getString("ngu_hang"));
			if (!jsonObj.isNull("age"))
				zodiac.settuoi(jsonObj.getString("age"));
			if (!jsonObj.isNull("avatar"))
				zodiac.setavatar(jsonObj.getString("avatar"));
			zodiac.showinfo();
			listZodiac.add(zodiac);
		}
		return listZodiac;
	}

	public static ArrayList<XongdatObject> readxongdat(String result, String key) throws JSONException {
		JSONObject jsonObject = new JSONObject(result);
		ArrayList<XongdatObject> listZodiac = new ArrayList<XongdatObject>();
		JSONArray jsonArrayMenu = jsonObject.getJSONArray(key);
		for (int i = 0; i < jsonArrayMenu.length(); i++) {
			JSONObject jsonObj = jsonArrayMenu.getJSONObject(i);
			XongdatObject zodiac = new XongdatObject();
			if (!jsonObj.isNull("GIAP"))
				zodiac.settitle(jsonObj.getString("GIAP"));
			if (!jsonObj.isNull("YEAR"))
				zodiac.setyear(jsonObj.getString("YEAR"));
			if (!jsonObj.isNull("SCALE"))
				zodiac.setrate(jsonObj.getString("SCALE"));
			if (!jsonObj.isNull("AVATAR"))
				zodiac.setimage(jsonObj.getString("AVATAR"));
			zodiac.showinfo();
			listZodiac.add(zodiac);
		}
		return listZodiac;
	}

	public static ArrayList<XongdatObject> readxongdatngay(String result) throws JSONException {
		JSONObject jsonObject = new JSONObject(result);
		ArrayList<XongdatObject> listZodiac = new ArrayList<XongdatObject>();
		JSONArray jsonArrayMenu = jsonObject.getJSONArray("XONG");
		for (int i = 0; i < jsonArrayMenu.length(); i++) {
			JSONObject jsonObj = jsonArrayMenu.getJSONObject(i);
			XongdatObject zodiac = new XongdatObject();
			if (!jsonObj.isNull("ngay"))
				zodiac.setdate(jsonObj.getString("ngay"));
			if (!jsonObj.isNull("gio"))
				zodiac.settime(jsonObj.getString("gio"));
			zodiac.showinfo();
			listZodiac.add(zodiac);
		}
		return listZodiac;
	}
}
